Mock.mock("/js/useradd","post",function(user){
    console.log(JSON.parse(user.body));
    user=JSON.parse(user.body)
    let bool=false
            let arr=[]
            let UserNewString=localStorage.UserNew
            if(UserNewString==undefined || UserNewString==''||UserNewString==null){
                let MaxID=1
                user.id=MaxID
                MaxID++
                localStorage.MaxId=JSON.stringify(MaxID)
                arr.push(user)
                localStorage.UserNew=JSON.stringify(arr)
            }else{
                var valueone=user.username
                let arrtwo=JSON.parse(localStorage.UserNew)
                for (const iterator of arrtwo) {
                    if(iterator.username==valueone){
                        return alert('用户名已存在')
                    }
                }
                MaxID=JSON.parse(localStorage.MaxId)
                user.id=MaxID
                MaxID++
                localStorage.MaxId=JSON.stringify(MaxID)
                let arr=JSON.parse(localStorage.UserNew)
                arr.push(user)
                localStorage.UserNew=JSON.stringify(arr)
            }
                  window.location.href='./login.html'
})

Mock.mock("/js/userlogin","post",function(obj){
    console.log(JSON.parse(obj.body));
    acount=JSON.parse(obj.body).username
    psw=JSON.parse(obj.body).password
    users=JSON.parse(localStorage.UserNew)
    console.log(users);
    for (const iterator of users) {
        if(acount==iterator.username&&psw==iterator.password){
            var date=new Date()
            date.setTime(date.getTime()+1000*60*60*24)
            document.cookie=`user=${JSON.stringify(iterator)};expires=${date}`
            return location.href='./roomshow.html'
        }
    }
    return alert('acount or passwrod error')
    
})

Mock.mock("/js/roomadd","post",function(room){
    console.log(JSON.parse(room.body));
    room=JSON.parse(room.body)
    if(localStorage.NewRoom==undefined||localStorage.NewRoom==''||localStorage.NewRoom=='[]'){
                let MaxID=1
                room.id=MaxID
                MaxID++
                localStorage.RoomMaxId=JSON.stringify(MaxID)
                arr.push(room)
                localStorage.NewRoom=JSON.stringify(arr)
            }else{
                var valueone=room.name
                let arrtwo=JSON.parse(localStorage.NewRoom)
                for (const iterator of arrtwo) {
                    if(iterator.name==valueone){
                        return alert('茶叶名已存在')
                    }
                }
                MaxID=JSON.parse(localStorage.RoomMaxId)
                room.id=MaxID
                MaxID++
                localStorage.RoomMaxId=JSON.stringify(MaxID)
                arr=JSON.parse(localStorage.NewRoom)
                arr.push(room)
                localStorage.NewRoom=JSON.stringify(arr)
            }
})

Mock.mock("/js/selectAll","get",function(){
    if(localStorage.NewRoom==undefined||localStorage.NewRoom==''||localStorage.NewRoom=='[]'){
        var newtd=document.createElement('tr')
        newtd.innerHTML=`
        <td></td>
        <td>暂无数据</td>
        <td>暂无数据</td>
        <td>暂无数据</td>
        <td>暂无数据</td>
        <td>暂无数据</td>
        `
        var tableobj=document.getElementsByTagName('table')[0]
        tableobj.appendChild(newtd)
    }else{
        var allroom=JSON.parse(localStorage.NewRoom)
       for (const iterator of allroom) {
            var newtd=document.createElement('tr')
            newtd.innerHTML=`
            <td><input type='checkbox' value='${iterator.id}' class='mycheckbox' ></td>
            <td><img src='${iterator.img}' alt="" style="width: 100px;height: 100px;"></td>
            <td>${iterator.name}</td>
            <td>${iterator.lei}</td>
            <td>${iterator.price}</td>
            <button onclick='upd(${iterator.id})'>修改</button>
            <button onclick='del(${iterator.id})'>删除</button>
            `
        var tableobj=document.getElementsByTagName('table')[0]
        tableobj.appendChild(newtd)
  
    }   
    }
    
    var inputlist=document.getElementsByClassName('mycheckbox')
    //1.获取div里面的input标签对象
    var inpobj=document.getElementById("inpt");
      //给每一个checkbox 添加改变事件
      for(var i=0;i<inputlist.length;i++){
        inputlist[i].onchange=function(){
            console.log(i);
            var cont=0;
        //查询chebox的状态如果为全部选中，更改全选框的状态  如果其中一个没有被选中，那么全选框就不能选中
        for(var i=0;i<inputlist.length;i++){
            if(inputlist[i].checked==true){
                //累加器
                cont++;
            }
        }
        //在循环外面判断cont的值  如果cont的值跟inputlist的长度一致。表示全部选中
        if(cont==inputlist.length){
            inpobj.checked=true;
        }else{
            inpobj.checked=false;
        }
        }
    }
})

Mock.mock("/js/roomdelete","post",function(obj){
    roomid=JSON.parse(obj.body)
    var allroom=JSON.parse(localStorage.NewRoom)
            for(i=0;i<allroom.length;i++){
                var room=allroom[i]
                if(room.id==roomid){
                    allroom.splice(i,1)
                    localStorage.NewRoom=JSON.stringify(allroom)
                    location.reload()
                }
            } 
    
})
Mock.mock("/js/roomdeleteall","post",function(obj){

    roomids=JSON.parse(obj.body)
    for (const iterator of roomids) {
        console.log(iterator);  
        var allroom=JSON.parse(localStorage.NewRoom)
            for(i=0;i<allroom.length;i++){
                var room=allroom[i]
                if(room.id==iterator){
                    allroom.splice(i,1)
                    localStorage.NewRoom=JSON.stringify(allroom)
                    location.reload()
                }
            } 
    }
  
    
})
Mock.mock("/js/selectroom","get",function(){
    function havenot(){
        var newtd=document.createElement('tr')
        newtd.innerHTML=`
        <td>暂无数据</td>
        <td>暂无数据</td>
        <td>暂无数据</td>
        <td>暂无数据</td>
        <td>暂无数据</td>
        `
        var tableobj=document.getElementsByTagName('table')[0]
        tableobj.appendChild(newtd)
    }
        if(localStorage.selectId==undefined){
            havenot()
        }else{
            var bool=false
            var roomid=localStorage.selectId
            var allroom=JSON.parse(localStorage.NewRoom)
           for (const iterator of allroom) {
            var room=iterator.name
            if(room===roomid){
                bool=true
                 var newtd=document.createElement('tr')
                newtd.innerHTML=`
                <td><img src='${iterator.img}' alt="" style="width: 100px;height: 100px;"></td>
                <td>${iterator.name}</td>
                <td>${iterator.lei}</td>
                <td>${iterator.price}</td>
            `
            var tableobj=document.getElementsByTagName('table')[0]
            tableobj.appendChild(newtd)
            }
           
      
        }
        if(!bool){
            havenot()
        }  
                
    }
    
})

Mock.mock("/js/roombeforeupdate","get",function(){
            var roomd=JSON.parse(localStorage.NewupdRoom)
            var leilist=document.getElementById('lei').options
            for (const key in leilist) {
                if(leilist[key].innerText==roomd.lei){
                        leilist[key].selected=true
                    }
            }
            document.getElementsByTagName('input')[0].value=roomd.name
            document.getElementsByTagName('input')[1].value=roomd.price  
})
Mock.mock("/js/roomupdate","post",function(obj){
    room=JSON.parse(obj.body)
    if(localStorage.NewRoom==undefined||localStorage.NewRoom=='[]'){
        let MaxID=1
        room.id=MaxID
        room.img='https://ccdn1.goodq.top/caches/9b69daffc17579b848483c854813cad2/aHR0cHM6Ly81Y2RkM2FhNWM0NmVhLnQ3My5xaWZlaXllLmNvbS9xZnktY29udGVudC91cGxvYWRzLzIwMTkvMDQvODMxNGZmNmYyYjkyNzcxZmY2N2U5NjdlZjQyNjAzZWItMTAyNHgxMDI0LTkwLndlYnA_p_p100_p_3D.webp'
        MaxID++
        localStorage.RoomMaxId=JSON.stringify(MaxID)
        arr.push(room)
        localStorage.NewRoom=JSON.stringify(arr)
    }else{
        MaxID=JSON.parse(localStorage.RoomMaxId)
        room.id=MaxID
        room.img='https://ccdn1.goodq.top/caches/9b69daffc17579b848483c854813cad2/aHR0cHM6Ly81Y2RkM2FhNWM0NmVhLnQ3My5xaWZlaXllLmNvbS9xZnktY29udGVudC91cGxvYWRzLzIwMTkvMDQvODMxNGZmNmYyYjkyNzcxZmY2N2U5NjdlZjQyNjAzZWItMTAyNHgxMDI0LTkwLndlYnA_p_p100_p_3D.webp'
        MaxID++
        localStorage.RoomMaxId=JSON.stringify(MaxID)
        arr=JSON.parse(localStorage.NewRoom)
        var roomd=JSON.parse(localStorage.NewupdRoom)
        for (let i = 0; i < arr.length; i++) {
            if(JSON.stringify(arr[i])==JSON.stringify(roomd)){
                arr.splice(i,1)
                arr.push(room)
            }
        }
        localStorage.NewRoom=JSON.stringify(arr)
    }
})
Mock.mock("/js/buyroom","post",function(obj){
    room=JSON.parse(obj.body)
    let arr=[]
            if(localStorage.MyRoom==undefined||localStorage.MyRoom=='[]'){
                arr.push(room)
                localStorage.MyRoom=JSON.stringify(arr)
            }else{
                arr=JSON.parse(localStorage.MyRoom)
                for (const iterator of arr) {
                    console.log(iterator.id);
                    console.log(room.id);
                    if(iterator.id==room.id){
                        return alert('已购买')
                    }
                }
                arr.push(room)
                localStorage.MyRoom=JSON.stringify(arr)
            }
})
Mock.mock("/js/Mycar","get",function(){
    var arrmoney=[]
    function havenot(){
        var newtd=document.createElement('tr')
        newtd.innerHTML=`
        <td>暂无数据</td>
        <td>暂无数据</td>
        <td>暂无数据</td>
        <td>暂无数据</td>
        <td>暂无数据</td>
`
        var tableobj=document.getElementsByTagName('table')[0]
        document.getElementById('enddo').innerText=`商品总数量0件,商品总价为0元`
        tableobj.appendChild(newtd)
    }
    if(localStorage.selectId==undefined){
        havenot()
    }else{
        var bool=false
        var allroom=JSON.parse(localStorage.MyRoom)
        var shuliang=0
        var allprice=0
       for (const iterator of allroom) {
            shuliang+=1
            allprice+=parseInt(iterator.price)
            bool=true
            var newtd=document.createElement('tr')
            arrmoney.push(iterator.price)
            newtd.innerHTML=`
            <td><img src='${iterator.img}' alt="" style="width: 100px;height: 100px;"></td>
            <td>${iterator.name}</td>
            <td>${iterator.price}</td>
            <td> <button id='one'>-</button>
                <input type='number' value='1'>
                <button id='add'>+</button>
            </td>
            <td><p>${iterator.price}元</p></td>
            `
        var tableobj=document.getElementsByTagName('table')[0]
        tableobj.appendChild(newtd)
    }
     document.getElementById('enddo').innerText=`商品总数量${shuliang}件,商品总价为${allprice}元`
    if(!bool){
        havenot()
    }       
}
allone= document.querySelectorAll('#one')
alladd= document.querySelectorAll('#add')
allinp=document.getElementsByTagName('input')
function checkallmoney(){
    var alltitle=0
    var allmoney=0
    for(i=0;i<allinp.length;i++){
        console.log(allinp[i].value);
        alltitle+=parseInt(allinp[i].value)
        allmoney+=parseInt(allinp[i].value)*arrmoney[i]
    }
    document.getElementById('enddo').innerText=`商品总数量${alltitle}件,商品总价为${allmoney}元`
}
for (var i = 0; i < allone.length; i++) {
    // 使用IIFE捕获当前的i值
    (function(currentIndex) {
        allone[currentIndex].onclick = function() {
            // 当点击事件触发时，currentIndex将是循环迭代时的i值
            allinpdo(currentIndex);
        };
        alladd[currentIndex].onclick = function() {
                // 当点击事件触发时，currentIndex将是循环迭代时的i值
            alladddo(currentIndex);
        };
        allinp[currentIndex].onblur = function() {
                // 当点击事件触发时，currentIndex将是循环迭代时的i值
            if(allinp[currentIndex].value<1){
                alert('不允许比1小')
                document.getElementsByTagName('input')[currentIndex].value=1
            }else{
                document.getElementsByTagName('p')[currentIndex].innerHTML=`${arrmoney[currentIndex]*allinp[currentIndex].value}元`
                checkallmoney()
            }
        };
    })(i); // 立即执行函数，并将当前的i值作为参数传递

    }
return arrmoney
})

